﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace Qcy.Jshj.Web.Models
{
    public class ProductCategory
    {
        [Key]
        public int Id { get; set; }

        public ProductCategory Parent { get; set; }
        public string Name { get; set; }
        public string NameEn { get; set; }
        public string FullName
        {
            get
            {
                if (this.Parent != null)
                {
                    return this.Parent.Name + "－" + Name;
                }
                return this.Name;
            }
        }
        public string FullNameEn
        {
            get
            {
                if (this.Parent != null)
                    return string.Format("{0}-{1}", this.Parent, this.Name);
                return this.Name;
            }
        }
        public string Description { get; set; }
        public string DescriptionEn { get; set; }

        public static IList<ProductCategory> All
        {
            get
            {
                IList<ProductCategory> _All = null;

                using (var context = new JshjDataContext())
                {
                    var source = context.ProductCategory;
                    _All = new List<ProductCategory>();
                    foreach (var item in source)
                    {
                        if (item.Parent == null)
                        {
                            if (source.Count(c => c.Parent != null && c.Parent.Id == item.Id) == 0)
                                _All.Add(item);
                        }
                        else
                        {
                            _All.Add(item);
                        }
                    }
                }
                return _All;
            }
        }
    }
}